export default {
  namespaced: true,
  state: {
    userMessageList: [],
    teamMessageList: [],
    taskMessageList: [],
  },
  mutations: {
    setUserMessageList(state, value) {
      state.userMessageList = [...state.userMessageList, ...value];
      state.userMessageList.sort(
        (a, b) => b.createTime.getTime() - a.createTime.getTime()
      );
    },
    setTeamMessageList(state, value) {
      state.teamMessageList = [...state.teamMessageList, ...value];
      state.teamMessageList.sort(
        (a, b) => b.createTime.getTime() - a.createTime.getTime()
      );
    },
    setTaskMessageList(state, value) {
      state.taskMessageList = [...state.taskMessageList, ...value];
      state.taskMessageList.sort(
        (a, b) => b.createTime.getTime() - a.createTime.getTime()
      );
    },

    clearMessageList(state, type) {
      switch (type) {
        case 0:
          state.userMessageList = [];
          break;
        case 1:
          state.teamMessageList = [];
          break;
        case 2:
          state.taskMessageList = [];
          break;
      }
    },
  },
  actions: {
    // 获取消息列表
    getMessageList({ commit }, payload) {
      const db = wx.cloud.database(),
        { type, userId, page, limitCount } = payload;
      return new Promise(async (resolve, reject) => {
        try {
          const res = await db
            .collection("message")
            .where({
              type,
              receiveId: userId,
            })
            .orderBy("createTime", "desc")
            .skip(page * limitCount)
            .limit(limitCount)
            .get();
          console.log("查询结果：", res);
          switch (type) {
            case 0:
              commit("setUserMessageList", res.data);
              break;
            case 1:
              commit("setTeamMessageList", res.data);
              break;
            case 2:
              commit("setTaskMessageList", res.data);
              break;
            default:
              console.log("type传入有误");
          }
          resolve("获取消息列表成功");
        } catch (err) {
          console.log("获取消息列表失败:", err);
          reject(err);
        }
      });
    },
    // 发送消息
    sendMessage({}, item) {
      const db = wx.cloud.database(),
        _ = db.command;
      return new Promise(async (resolve, reject) => {
        try {
          await db.collection("message").add({
            data: item,
          });
          resolve("申请消息发送成功");
        } catch (err) {
          console.log("申请发送消息失败：", err);
          reject(err);
        }
      });
    },

    // 更改消息已读/已处理
    updateMessageStatus({}, payload) {
      const db = wx.cloud.database(),
        { isView, isHandle, messageId } = payload;
      return new Promise(async (resolve, reject) => {
        try {
          await db.collection("message").where({ _id: messageId }).update({
            data: {
              isView,
              isHandle,
            },
          });
          resolve("更改消息状态成功");
        } catch (err) {
          console.log("更改信息状态失败：", err);
          reject(err);
        }
      });
    },

    // 移除指定的消息
    removeMessage({}, messageId) {
      const db = wx.cloud.database();
      return new Promise(async (resolve, reject) => {
        try {
          await db.collection("message").where({ _id: messageId }).remove();
          resolve("删除成功");
        } catch (err) {
          console.log("删除消息失败：", err);
          reject(err);
        }
      });
    },
  },
};
